package com.cskaoyan.demo9.service;

import com.cskaoyan.demo9.bean.PageData;
import com.cskaoyan.demo9.bean.PageParameter;
import com.cskaoyan.demo9.mapper.MarketUserMapper;
import com.cskaoyan.demo9.model.MarketUser;
import com.cskaoyan.demo9.model.MarketUserExample;
import com.cskaoyan.demo9.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    @Override
    public PageData list(PageParameter pageParameter, String username, String mobile) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketUserMapper userMapper = sqlSession.getMapper(MarketUserMapper.class);


        // 分页
        PageHelper.startPage(pageParameter.getPage(), pageParameter.getLimit());
        //List<MarketUser> userList = userMapper.selectAll();
        MarketUserExample example = new MarketUserExample();
        // 排序
        example.setOrderByClause(pageParameter.getSort() + " " + pageParameter.getOrder());
        // 条件查询 → 拼接条件
        // 创建条件列表
        MarketUserExample.Criteria criteria = example.createCriteria();
        // 根据传入的请求参数 决定是否拼接条件
        if (username !=null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (mobile != null && !"".equals(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }

        List<MarketUser> userList = userMapper.selectByExample(example);

        // 封装分页信息
        PageData data = PageData.list(userList);
        sqlSession.close();
        return data;
    }
}
